package com.example.demo.service.impl;

import com.example.demo.domain.ProductOrder;
import com.example.demo.service.ProductClient;
import com.example.demo.service.ProductOrderService;
import com.example.demo.util.JsonUtils;
import com.fasterxml.jackson.databind.JsonNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Date;
import java.util.UUID;

@Service
public class ProductOrderServiceImpl implements ProductOrderService {


    @Autowired
    private RestTemplate restTemplate;
    @SuppressWarnings("SpringJavaAutowiringInspection")
    @Autowired
    private ProductClient productClient;

    @Override
    public ProductOrder save(int userId, int productId) {

        Object obj = restTemplate.getForObject("http://product-service/api/v1/product/getById?id="+productId, Object.class);

        System.out.println(obj);

        ProductOrder productOrder = new ProductOrder();
        productOrder.setCreateTime(new Date());
        productOrder.setUserId(userId);
        productOrder.setTradeNo(UUID.randomUUID().toString());

        return productOrder;
    }
    //用FeignClient方式实现
    @Override
    public ProductOrder save2(int userId, int productId) {

        String strProduct = productClient.findById(productId);
        JsonNode jsonNode = JsonUtils.str2JsonNode(strProduct);
        System.out.println(strProduct);

        ProductOrder productOrder = new ProductOrder();
        productOrder.setCreateTime(new Date());
        productOrder.setUserId(userId);
        productOrder.setTradeNo(UUID.randomUUID().toString());
        productOrder.setProductName(jsonNode.get("name").toString());
        System.out.println(productOrder.getProductName());


        return productOrder;
    }
}
